package com.world.common.model;

import com.world.common.generate.auto.Action;
import com.world.common.generate.auto.QField;
import com.world.common.util.DateUtil;
import com.world.common.util.SessionUtil;
import com.world.common.util.SysUtil;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 基础类 继承它的注意数据库字段
 */
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = -707273279554634993L;
    public static final Long VERSION_DEFAULT = 1L;

    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "uuid")
    @Column(length = 50)
    private String id;
    @Column(name = "creator", updatable = false, nullable = false, length = 50)
    @QField(name = "创建人", actions = {Action.query})
    private String creator;
    @Column(name = "modifier", nullable = false, length = 50)
    private String modifier;
    @Column(name = "create_time", updatable = false, nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;
    @Column(name = "modify_time", nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date modifyTime;
    @Version
    @Column(name = "version")
    private Long _version;

    @Transient
    @QField(name = "创建时间", actions = {Action.export})
    private String createTimeStr;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getModifier() {
        return modifier;
    }

    public void setModifier(String modifier) {
        this.modifier = modifier;
    }

    protected Long getVersion() {
        return this._version;
    }

    private void setVersion(Long version) {
        this._version = version;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public Long get_version() {
        return _version;
    }

    public void set_version(Long _version) {
        this._version = _version;
    }

    public String getCreateTimeStr() {
        return DateUtil.format("yyyy-MM-dd HH:mm:ss", this.getCreateTime());
    }

    public void setCreateTimeStr(String createTimeStr) {
        this.createTimeStr = createTimeStr;
    }

    @PrePersist
    public void initCreation() {
        if (getCreateTime() == null) {
            setCreateTime(new Date());
        }
        if (getCreator() == null) {
            setCreator(SysUtil.isDevMode() ? "admin" : SessionUtil.getUserName());
        }
        if (getModifyTime() == null) {
            setModifyTime(new Date());
        }
        if (getModifier() == null) {
            setModifier(SysUtil.isDevMode() ? "admin" : SessionUtil.getUserName());
        }
    }

    @PreUpdate
    public void initModify() {
        setModifyTime(new Date());
        setModifier(SessionUtil.getUserName());
    }

    public Object toDTO() {
        return null;
    }
}